/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.HangGiaCongDTO;

/**
 *
 * @author tuan
 */
public class HangGiaCongDAO {
    private static final String MaHangGC = "MaHGC000";
    private static final int lengthMa = 8;
    
    public HangGiaCongDAO() {
    }
    
    public String getNewMaHangGC() throws SQLException {
        String MaHGC = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllHangGCFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAHANGGC").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaHGC = MaHGC.concat(MaHangGC + maxMa);
        return MaHGC;
    }
    
    public HangGiaCongDTO getHangGiaCongByMaHGC(String MaHGC) throws SQLException {
        Object[] objs = { MaHGC };
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetMachinedGoodFromDatabase(?)", objs);
        HangGiaCongDTO hangGC = new HangGiaCongDTO();
        while (rs.next()){
            hangGC.setMaHangGC(rs.getString("MAHANGGC"));
            hangGC.setTenGiaCong(rs.getString("TENGC"));
            hangGC.setDonGia(rs.getInt("DONGIA"));
        }
        
        return hangGC;
    }
    public int updateHangGC(HangGiaCongDTO hangGC) {
        Object[] objs = { hangGC.getMaHangGC(), hangGC.getTenGiaCong(), hangGC.getDonGia() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateHangGiaCongFromDataBase(?, ?, ?)", objs);
    }
    
    public int deleteHangGCByMa(String MaHGC) {
        Object[] objs = { MaHGC };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteHangGCByMaHGCToDatabase(?)", objs);
    }
    
    public int insertHangGC(HangGiaCongDTO hangGC) {
        Object[] objs = { hangGC.getMaHangGC(), hangGC.getTenGiaCong(), hangGC.getDonGia() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertMachinedGoodBillFromDatabase(?, ?, ?)", objs);
    }
}
